package asia.hymsk.spring.service.impl;

import asia.hymsk.spring.dao.ProductRecsRepository;
import asia.hymsk.spring.dao.ProductRepository;
import asia.hymsk.spring.dao.RatingRepository;
import asia.hymsk.spring.mapper.UserMapper;
import asia.hymsk.spring.pojo.ProductData;
import asia.hymsk.spring.pojo.Response;
import asia.hymsk.spring.pojo.mongodb.Product;
import asia.hymsk.spring.pojo.mongodb.ProductRecs;
import asia.hymsk.spring.pojo.mongodb.Rating;
import asia.hymsk.spring.pojo.mongodb.Recs;
import asia.hymsk.spring.service.ProductService;
import asia.hymsk.spring.service.kafka.KafkaLogProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Stream;

/**
 * @author 花于陌上开
 * @date 2023-06-27 23:17
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductRepository productRepository;
    @Autowired
    RatingRepository ratingRepository;
    @Autowired
    ProductRecsRepository productRecsRepository;
    @Autowired
    UserMapper userMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public Response doMark(Integer productId, String userName, Double mark) {
        //插入(评分表插入信息)
        if (mark > 5.0 || mark < 0.0) {
            return Response.error("评分分值有误，分值应在0~5之间");
        }

        Integer userId = userMapper.selectByName(userName).getUserId();
        //Redis
        if (stringRedisTemplate.hasKey("userId:" + userId) && stringRedisTemplate.opsForList().size("userId:" + userId)>= 40) {
            stringRedisTemplate.opsForList().rightPop("userId:" + userId);
        }
        stringRedisTemplate.opsForList().leftPush("userId:" + userId, productId + ":" + mark);
        //进入消息队列
        String text = userId + "|" + productId + "|" + mark + "|" + System.currentTimeMillis() / 1000;
        KafkaLogProducer.produceLog(text);
        //数据库中评分
        Rating rating = new Rating(null,mark, userId, productId, System.currentTimeMillis());
        ratingRepository.insert(rating);
        return Response.success("评分成功");
    }

    @Override
    public Response dataList(Integer productId) {
        //查询(查询商品信息列表,包括商品相似度列表)
        Product product = productRepository.findByProductId(productId);
        return Response.success("请求成功", product);
    }

    @Override
    public Response dataDetails(Integer productId) {
        //查询(查询商品信息列表,包括商品相似度列表)
        Product product = productRepository.findByProductId(productId);
        ProductRecs productRecs=productRecsRepository.findByProductId(productId);
        Stream<Recs> recommendStream= Arrays.stream(productRecs.getRecs()).sorted(Comparator.comparingDouble(Recs::getScore).reversed());
        productRecs.setRecs(recommendStream.toArray(Recs[]::new));
        return Response.success("请求成功", new ProductData(product,productRecs));
    }

    @Override
    public Response publishProduct(String userName, String productName, String picture, String productDesc, String[] productType) {
        //插入商品信息
        Integer userId = userMapper.selectByName(userName).getUserId();
        return null;
    }

    @Override
    public Response alterProduct(String userName, String productName, String picture, String productDesc, String[] productType) {
        //修改商品信息
        return null;
    }

    @Override
    public Response publishList(String userName) {
        //获取用户商品发布列表
        return null;
    }
}
